﻿
using System;
using System.Data;
using System.Collections.Generic;
using BYFW.SFL;

namespace LDSP.BRL
{
    public partial class EnterpriseUser : BYFW.BOL.BaseEntity
    {
        public EnterpriseUser()
        {
            m_tableName = "t_EnterpriseUser";
            m_hasName = true;
            m_hasComment = true;
            m_tblCacheManager.IsEnable = true;
            m_objCacheManager.Size = 1000;
        }

        protected string m_code = string.Empty;
        protected string m_password = string.Empty;
        protected EnterpriseUserType m_userType = EnterpriseUserType.企业管理员;
        protected int m_enterpriseID = 0;
        protected Enterprise m_enterprise = null;
        protected bool m_isValid = true;
        protected bool m_supportManageFavorite = false;
        protected bool m_supportSMS = false;
        protected string m_email = string.Empty;

        /// <summary>
        /// 密码
        /// </summary>
        public string Code
        {
            get { return m_code; }
            set { m_code = value; }
        }
        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get
            {
                if (m_password.Length > 1)
                {
                    return CommonOP.DecryptString(m_password.Substring(1), false);
                }
                else
                {
                    return m_password;
                }
            }
            set
            {
                m_password = CommonOP.Spliter + CommonOP.EncryptString(value);
            }
        }
        /// <summary>
        /// 企业用户类型
        /// </summary>
        public EnterpriseUserType UserType
        {
            get { return m_userType; }
            set { m_userType = value; }
        }
        /// <summary>
        /// 企业ID
        /// </summary>
        public int EnterpriseID
        {
            get { return m_enterpriseID; }
            set { m_enterpriseID = value; }
        }
        /// <summary>
        /// 是否有效
        /// </summary>
        public bool IsValid
        {
            get { return m_isValid; }
            set { m_isValid = value; }
        }
        /// <summary>
        /// 是否允许管理适用法规
        /// </summary>
        public bool SupportManageFavorite
        {
            get { return m_supportManageFavorite; }
            set { m_supportManageFavorite = value; }
        }
        /// <summary>
        /// 是否允许接收短信
        /// </summary>
        public bool SupportSMS
        {
            get { return m_supportSMS; }
            set { m_supportSMS = value; }
        }
        /// <summary>
        /// 邮箱
        /// </summary>
        public string Email
        {
            get { return m_email; }
            set { m_email = value; }
        }

        public Enterprise Enterprise
        {
            get
            {
                if (m_enterprise == null)
                {
                    m_enterprise = new Enterprise();
                    if (m_enterpriseID != 0)
                    {
                        m_enterprise.GetProperties(m_enterpriseID);
                    }
                }
                return m_enterprise;
            }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("code", m_code);
            m_container.Add("password", m_password);
            m_container.Add("userType", (int)m_userType);
            m_container.Add("enterpriseID", m_enterpriseID);
            m_container.Add("isValid", m_isValid ? 1 : 0);
            m_container.Add("supportManageFavorite", m_supportManageFavorite ? 1 : 0);
            m_container.Add("supportSMS", m_supportSMS ? 1 : 0);
            m_container.Add("email", m_email);
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_code = this.GetStringValue(record, "code");
            m_password = this.GetStringValue(record, "password");
            m_userType = (EnterpriseUserType)this.GetIntValue(record, "userType");
            m_enterpriseID = this.GetIntValue(record, "enterpriseID");
            m_isValid = this.GetBoolValue(record, "isValid");
            m_email = this.GetStringValue(record, "email");
            m_supportManageFavorite = this.GetBoolValue(record, "supportManageFavorite");
            m_supportSMS = this.GetBoolValue(record, "supportSMS");
            return true;
        }

        public bool CanUpdate(string transID)
        {
            string condition = string.Format(
           "WHERE {0}={1} AND {2}<>{3}"
           , DB.GetCol("code"), DB.GetVal(m_code)
           , DB.GetCol(m_idField), DB.GetVal(m_id));
            return !this.Exist(transID, condition, false);
        }

        public bool GetProperties(string code)
        {
            string cond = string.Format("where {0}={1}", DB.GetCol("code"), DB.GetVal(code));
            DataTable dt = this.GetRecords(cond);
            return this.GetProperties(dt.Rows.Count > 0 ? dt.Rows[0] : null);
        }

        public void DeleteByEnterprise(string transID, int enterpriseID)
        {
            List<EnterpriseUser> userList = this.GetSpecialList<EnterpriseUser>(transID, "enterpriseID", enterpriseID);
            for (int i = 0; i < userList.Count; i++)
            {
                userList[i].Delete(transID);
            }
        }

        public override int Delete(string transID)
        {
            new R_CodificationUser().DeleteByUser(transID, m_id);
            return base.Delete(transID);
        }

        public DataTable GetEnterpriseReportUserPlan(int enterpriseID, string cond)
        {
            string sql = string.Format(@"
SELECT u.name, count(u.name) as number, t1.priority as priority from t_codification t, t_r_enterprise_codification t1, 
t_plan p, t_r_plan_user pu, t_enterpriseUser u
WHERE t.id=t1.docID
and p.favoriteid=t1.id
and pu.planID=p.id
and pu.userID=u.id
and {0}={1} {2} GROUP BY u.name, t1.priority",
    "t1.enterpriseID", DB.GetVal(enterpriseID), cond);

            return DB.Select(sql).Tables[0];
        }

        public DataTable GetEnterpriseGroupReportUserPlan(int enterpriseID, string cond)
        {
            string sql = string.Format(@"
SELECT e.code as name,t1.priority as priority ,count(t1.enterpriseID) as number 
from t_r_enterprise_codification t1, t_plan p, t_enterprise e
WHERE p.favoriteid=t1.id
and t1.enterpriseID = e.id
and t1.enterpriseID in (select id  from t_enterprise where parentID={0}) 
GROUP BY e.code, t1.priority
{1}", DB.GetVal(enterpriseID), cond);

            return DB.Select(sql).Tables[0];
        }

        public DataTable GetRecordsByEnterpriseName(string enterpriseName, string userType, string userCode)
        {
            int t = 0;
            string type = Int32.TryParse(userType, out t) ? userType : string.Empty;
            string sql = string.Format(@"
select t1.* 
from t_enterpriseUser t1 inner join t_enterprise t2 on t1.enterpriseID = t2.id
where t1.code like '%{0}%' and t2.code like '%{1}%'",
                                      userCode,
                                      enterpriseName);
            sql += !string.IsNullOrEmpty(type) ? " and userType = " + userType : string.Empty;

            return this.DB.Select(sql).Tables[0];
        }

        public List<EnterpriseUser> GetEnterpriseUserByEnterpriseId(int enterpriseId)
        {
            return this.GetSpecialList<EnterpriseUser>(null, "enterpriseID", enterpriseId);
        }

        public void UpdatePwdToEncrypted()
        {
            string cond = string.Format("WHERE {0} not like N'{1}%'", DB.GetCol("password"), CommonOP.Spliter);
            var list = this.GetSpecialList<EnterpriseUser>(null, cond);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Password = list[i].m_password;
                list[i].Update();
            }
        }
    }
}